package controllers

import (
	"net/http"
	"encoding/json"
	"git.oschina.net/YPTArch/processcache/sys/logs"
	"git.oschina.net/YPTArch/processcache/models"
)


//用户登出
func Logout(w http.ResponseWriter, r *http.Request)   {
	logs.Logger.Debug("success in logout")

	var result models.Result

	reply, err := logoutHandler(r)
	if err != nil {
		result = models.Result{Code: models.Unauthorized, Msg: "登出处理失败！", Data: err.Error()}
	} else {
		result = models.Result{Code: models.Ok, Msg: "登出成功！", Data: reply}
	}

	re, err := json.Marshal(result)
	if err != nil {
		logs.Logger.Error(err.Error())
		w.Write([]byte(models.ServerErr))
	}

	w.Write(re)
}

//redis删除用户
func logoutHandler(r *http.Request) (reply int, err error) {

	tokenRequest, err := validateUser(r)
	if err != nil {
		return
	}

	reply, err = models.Remove(USER+tokenRequest.UserId)
	if err != nil {
		return
	}
	return
}